package com.hoshiicloud.market.service.impl.market;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.market.ShareInfo;
import com.hoshiicloud.market.mapper.market.ShareInfoMapper;
import com.hoshiicloud.market.service.market.ShareInfoService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yangcheng
 * @version 1.0
 * @title: ShareInfoServiceImpl
 * @description: 分享信息Service接口实现类
 * @date 2019/8/2 11:52
 * @mail maiboyc@163.com
 */
@Service
public class ShareInfoServiceImpl  extends ServiceImpl<ShareInfoMapper, ShareInfo> implements ShareInfoService {

    /**
     * 根据ID获取
     * @param id 主键ID
     * @return ShareInfo
     */
    @Cacheable(cacheNames = "ShareInfo", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "营销服务-分享信息-查询分享信息 ")
    @Override
    public ShareInfo getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 新增
     * @param entity 新增对象
     * @return Long
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ShareInfo", allEntries = true)
    @Logable(authName = "营销服务-分享信息-新增分享信息")
    @Override
    public Long insert(ShareInfo entity) throws ServiceException{
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return entity.getId();
    }

    /**
     * 更新
     * @param entity 更新对象
     * @return Long
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ShareInfo", allEntries = true)
    @Logable(authName = "营销服务-分享信息管理-修改分享信息")
    @Override
    public Long update(ShareInfo entity) throws ServiceException{
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return entity.getId();
    }

    /**
     * 分页查询
     * @param page 分页参数
     * @param queryWrapper 查询参数
     * @return IPage<ShareInfo>
     */
    @Logable(authName = "营销服务-分享信息管理-分页查询分享信息")
    @Cacheable(cacheNames = "ShareInfo", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public IPage<ShareInfo> page(IPage<ShareInfo> page, Wrapper<ShareInfo> queryWrapper,String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Logable(authName = "营销服务-分享信息管理-查询分享信息")
    @Cacheable(cacheNames = "ShareInfo", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public List<ShareInfo> list(Wrapper<ShareInfo> queryWrapper, String key) {
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据ID删除
     * @param entity 删除对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ShareInfo", allEntries = true)
    @Logable(authName = "营销服务-分享信息管理-删除分享信息")
    @Override
    public boolean deleteById(ShareInfo entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }
}
